'use strict';
import React from 'react';
import { Route, Redirect, IndexRoute,IndexRedirect } from 'react-router';
import { cache_get } from './utils/browserCache.js';
import * as appActions from './reducers/app/actions.js';
import Properties from '../../config/properties.js';
const NotFound = React.createClass({render () {return <h1>404 Not Found</h1>;}});
import AuthPage from './containers/auth/index';
import App from './containers/app/index';
import Home from './containers/home/Home';
import { AccountInfo } from './containers/account/index';
import { UsersList,UsersRole } from './containers/user/index';

import {
    PageHome,PageEdit,PageFront
} from './containers/page/index';

export default function getRoutes(store){
    const init = (nextState, replace) => {
        //console.log(222);
        let { app } = store.getState();
        //let {pathname} = nextState.location;
        if(!app.isLogged){
            let isLogged = cache_get("app.isLogged");
            //console.log("isLogged",isLogged)
            if(isLogged)
                store.dispatch(appActions.loginSucess())
            else{
                replace({ pathname: "/auth/login", query: {}});
            }
        }
    };
    return (
        <Route>
            <Route path="/" onEnter={init}>
                <IndexRedirect to="home"/>
                <Route component={App}>
                    <Redirect from="home" to="account/info"/>
                    <Route path="home" component={Home}/>
                    <Route path="account">
                        <Route path="info" component={AccountInfo}/>
                        <Route path="password" component={AccountInfo}/>
                        <Route path="avatar" component={AccountInfo}/>
                    </Route>
                    <Route path="users">
                        <Route path="list" component={UsersList}/>
                        <Route path="role" component={UsersRole}/>
                    </Route>
                    <Route path="bim">
                        <Route path="active" component={UsersList}/>
                        <Route path="projects" component={UsersRole}/>
                    </Route>
                    <Route path="mobile">
                        <Route path="admin/page" component={PageHome}/>
                        <Route path="admin/page/:hash" component={PageEdit}/>
                    </Route>
                </Route>
            </Route>
            <Route path="/mobile">
                <Route path="page/:hash" component={PageFront}/>
            </Route>

            <Route path="/auth" component={AuthPage.PageAuth}>
                <Route path='login' component={AuthPage.PageAuthLogin} />
            </Route>
            <Route path="*" component={NotFound} status={404} />
        </Route>
    );
}